// pages/all/all.js
var app = getApp();
var config = app.globalData.config;
var utils = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    scrollTop: 0,
    tab: 0,
    pageIndex: 1,
    pageSize: 10,
    userId: null,
    dataList: [],
    isShowTips: true,
    isTop: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var userInfo = wx.getStorageSync('sUserInfo');
    !!userInfo && this.setData({
      userId: userInfo.id
    })
    var tab = options.tab || 0;
    var title = '我参与的';
    switch (tab) {
      case '0':
        title = '我参与的';
        break;
      case '1':
        title = '我发布的';
        this.getIsTop();
        break;
      case '2':
        title = '我中奖的';
        break;
    }
    wx.setNavigationBarTitle({
      title: title
    })
    this.setData({
      tab: tab
    })
    this.getList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    wx.showNavigationBarLoading();
    this.setData({
      pageIndex: 1,
      dataList: []
    })
    this.getList();
    wx.hideNavigationBarLoading();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    if (this.data.isShowTips) {
      return;
    }
    var page = this.data.pageIndex + 1;
    this.setData({
      pageIndex: page
    })
    this.getList();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  onPageScroll(event) {
    this.setData({
      scrollTop: event.scrollTop
    });
  },
  onChangeTab(e) {
    wx.setNavigationBarTitle({
      title: e.detail.title
    });
    this.setData({
      tab: e.detail.index,
      scrollTop: 0,
      pageIndex: 1,
      isShowTips: true,
      dataList: []
    });
    this.getList();
  },
  getList() {
    var that = this;
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    var userId = that.data.userId;
    if (userId == null) {
      return;
    }
    var url = config.joinListUrl;
    switch (that.data.tab + '') {
      case '0':
        url = config.joinListUrl;
        break;
      case '1':
        url = config.pushListUrl;
        break;
      case '2':
        url = config.winListUrl;
        break;
    }
    wx.request({
      url: url,
      data: {
        userId: that.data.userId,
        pageIndex: that.data.pageIndex,
        pageSize: that.data.pageSize
      },
      header: {},
      method: 'GET',
      success: function(res) {
        // 回调函数
        var moment_list = that.data.dataList;
        for (var i = 0; i < res.data.data.length; i++) {
          moment_list.push(res.data.data[i]);
        }
        // 设置数据
        res.data && that.setData({
          dataList: moment_list,
          isShowTips: res.data.data.length == 0 || res.data.data.length < that.data.pageSize
        })
      },
      fail: function(res) {},
      complete: function(res) {
        wx.hideLoading()
      },
    })
  },
  //是否能重新推荐
  getIsTop() {
    var that = this;
    wx.request({
      url: config.canPushUrl,
      method: "GET",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        userId: that.data.userId
      },
      success: function(resp) {
        if (resp.data.result == 1) {
          that.setData({
            isTop: resp.data.data == '1'
          })
        }
      },
      fail(res) {}
    })
  },
  //重新推荐
  onTop(e) {
    var that = this;
    wx.request({
      url: config.setHomeUrl,
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        userId: that.data.userId,
        activityId: e.currentTarget.id
      },
      success: function(resp) {
        if (resp.data.result == 1) {
          wx.showToast({
            title: '推荐成功',
            icon: 'none'
          })
          that.getIsTop()
        } else {
          wx.showToast({
            title: '推荐失败，请联系客服',
            icon: 'none'
          })
        }
      },
      fail: function(e) {}
    })
  }
})